package com.ruoyi.system.util;


import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @ClassName: LicenseTool
 * @Description: 与系统相关的一些常用工具方法. 目前实现的有：获取MAC地址、IP地址、C硬盘序列号
 * @author: huang
 * @date: 2018年5月1日 下午8:47:27
 */
public class LicenseUtils {
	/**
	 * 获取当前操作系统名称. return 操作系统名称 例如:windows xp,linux 等.
	 */
	public static String getOSName() {
		return System.getProperty("os.name").toLowerCase();
	}
//	/**
//	 * 对机器码进行MD5加密
//	 * @return
//	 * @throws IOException
//	 * @throws NoSuchAlgorithmException
//	 */
//	public static String EncoderByMd5(String str) throws NoSuchAlgorithmException, UnsupportedEncodingException {
//		return MD5Util.encodeByMD5(str);
//	}

	/**
	 * @param date
	 * @param day 想要获取的日期与传入日期的差值 比如想要获取传入日期前四天的日期 day=-4即可
	 * @return
	 */
	public static Date getSomeDay(Date date, int day) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.add(Calendar.DATE, day);
		return calendar.getTime();
	}

	/**
	 * @Title: bytesToString
	 * @Description: TODO
	 * @param arr
	 * @return
	 */
	public static String bytesToString(byte[] arr) {
		StringBuffer sb = new StringBuffer();
		for (int i = 0; i < arr.length; i++) {
			if (i == 0) {
				sb.append(arr[i]);
			} else {
				sb.append("|").append(arr[i]);
			}
		}
		return sb.toString();
	}
	/**
	 * @Title: stringToBytes
	 * @Description: TODO
	 * @param str
	 * @return
	 */
	public static byte[] stringToBytes(String str) {
		String[] arr = str.split("\\|");
		byte[] bytes = new byte[arr.length];
		try {
			for (int i = 0; i < arr.length; i++) {
				bytes[i] = Byte.valueOf(arr[i].trim());
			}
		} catch (NumberFormatException e) {
			e.printStackTrace();
		}
		return bytes;
	}
	/**
	 * @Title: ver_Time
	 * @Description: 验证时间有效性
	 * @param starttime
	 * @param endtime
	 * @return
	 * @throws java.text.ParseException
	 */
	public static Map<String, String> ver_Time(Long starttime, Long endtime, Long nowtime) throws java.text.ParseException {
		Map<String, String> result = new HashMap<>();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		String leftDays = "";
		if (nowtime >= endtime) {
			result.put("result", "failed");
		} else if (nowtime < starttime) {
			result.put("result", "success");
			leftDays = getDays(starttime, endtime);
			Date date = new Date(nowtime);
			Date endTime = getSomeDay(date, Integer.valueOf(leftDays));
			result.put("days", leftDays);
			result.put("endTime", sdf.format(endTime));
		} else {
			result.put("result", "success");
			leftDays = getDays(nowtime, endtime);
			result.put("days", leftDays);
			Date endTime = new Date(endtime);
			result.put("endTime", sdf.format(endTime));
		}
		return result;
	}
	/**
	 * @Title: getDays
	 * @Description: 获取剩余天数
	 * @param starttime
	 * @param endtime
	 * @return
	 */
	public static String getDays(long starttime, long endtime) {
		long between_days = (endtime - starttime) / (1000 * 3600 * 24);
		String days = String.valueOf(between_days);
		return days;
	}
}
